import java.util.Arrays;
import java.util.LinkedList;
import java.util.Stack;

public class MyStack implements IStack{
    //用数组实现栈

    public int[] elem;//栈的大小
    public int usedSize;//记录当中栈的元素

    public static final int MAX_SIZE = 10;//默认10的容量
    public MyStack() {
        elem = new int[MAX_SIZE];
        this.usedSize = 0;
    }
    @Override
    public void push(int val) {
        if(full()) {
            System.out.println("栈已满，无法操作");
        }
        elem[usedSize] = val;
        usedSize++;
    }
    private boolean full() {

        return usedSize == MAX_SIZE;
    }
    @Override
    public int pop() {
        if(empty()){
            System.out.print("栈为空，无法操作 ");
            return -1;
        }
        usedSize--;
        return elem[usedSize];
    }

    @Override
    public int peek() {
        //查看栈顶元素
        if(empty()){
            System.out.print("栈为空，无法操作 ");
            return -1;
        }
        int tmp = elem[usedSize-1];

        return tmp;
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public boolean empty() {
        return usedSize == 0;
    }
    //逆波兰求值
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            String str = tokens[i];
            if(isOpreator(str)) {
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch (str) {
                    case "+":
                        stack.push(num2+num1);
                        break;
                    case "-":
                        stack.push(num1-num2);
                        break;
                    case "*":stack.push(num2*num1);
                        break;
                    case "/":stack.push(num1/num2);
                        break;
                }
            }else {
                //不是字符就入栈
                int val = Integer.valueOf(str);
                stack.push(val);
            }
        }
        return stack.pop();//栈中的元素就是结果
    }
    private boolean isOpreator(String str) {
        //判断当前是否为字符
        if(str.equals("+")||str.equals("-")||str.equals("*")||str.equals("/")) {
            return true;
        }
        return false;
    }
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char str = s.charAt(i);
            if(str=='('||str=='['||str=='{') {
                stack.push(str);
            }else {
                if(stack.empty()) {
                    return false;
                }else {
                    char ch = stack.peek();//ch为左括号
                    if(ch=='('&&str==')'||ch=='['&&str==']'||ch=='{'&&str=='}') {
                        stack.pop();
                    }else {
                        return false;
                    }
                }
            }
        }

        if(!stack.empty()) {
            return false;
        }
        return true;

    }

}
